package cc.stonemarket.caricature.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateFormat;
import android.util.Log;

public final class DateUtil {
	
   private DateUtil() {}
   
   public static final String TAG = "DateUtil";

   /**
    * 把dateStr字符串转换成Date对象
    * @param formatStr dateStr的格式
    * @param dateStr 被转换字符串
    * @return Date
    */
   public static final Date parse(String formatStr,String dateStr) {
	  SimpleDateFormat format = new SimpleDateFormat(formatStr);
     try {
        return format.parse(dateStr);
     } catch (ParseException E) {
    	 Log.e(TAG, E.toString(), E);
    	 return null;
     }
   }
   
   /** 
    * 把指定对象，按format格式化成String 
    * @param format
    * @param formatOBJ
    * @return
    */
   public static final String format( String format, Object formatOBJ ) {
	   if( formatOBJ instanceof Date ){
		   return (String) DateFormat.format(format, (Date)formatOBJ);
	   }else if( formatOBJ instanceof Calendar ) {
		   return (String) DateFormat.format(format, (Calendar)formatOBJ);
	   }else {
		   return (String) DateFormat.format(format, (Long)formatOBJ);
	   }
   }
   
}
